﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using JESAI.Framework.Filters;
using JESAI.Framework.Middlewares;
using System;

namespace JESAI.Framework
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddJESAIFramework(this IServiceCollection services)
        {
            //添加过滤器
            services.Configure<MvcOptions>((opts) => {
                opts.Filters.Add<HttpResponseFilter>();
                opts.Filters.Add<ResultHandlerAttribute>();
                opts.Filters.Add<ExceptionHandlerAttribute>();
            });

            // 注册自定义的 RequestResponseLoggingMiddleware 中间件
            //services.AddTransient<LogMiddleware>();
            return services;
        }
        public static IApplicationBuilder UseJESAIFramework(this IApplicationBuilder app)
        {
            //app.Use(async (context, next) => { context.Request.EnableBuffering(); await next(context); });
            // 添加自定义的 RequestResponseLoggingMiddleware 中间件
            app.UseMiddleware<LogMiddleware>();
            return app;
        }
    }
}
